<template>
  <div id="printTest" style="margin:30px">
    <p class="font">出货日报表</p>
    <el-form ref="form" :model="form" label-width="80px" size="mini" :inline="true">
      <el-form-item label="出货日期:">
        <el-date-picker
          v-model="form.shipDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="客户名称:">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="出货单号:">
        <el-input v-model="form.outNo" />
      </el-form-item>
      <el-form-item label="">
        <el-button size="mini" type="primary" @click="query">查询</el-button>
        <el-button type="warning" size="mini" @click="print(1)">批量打印</el-button>
        <el-button type="success" size="mini" @click="toExcel">Excel导出</el-button><br>
      </el-form-item>
    </el-form>
    <span style="margin-left:25%">制表:{{ name }}</span>
    <span style="margin-left:400px">打印日期:{{ now }}</span><hr>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      border
      stripe
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="name" label="客户名称" width="120" />
      <el-table-column prop="shipDate" label="出货日期" width="120" />
      <el-table-column prop="outNo" label="出货单号" width="120" />
      <el-table-column prop="boxType" label="箱型" width="120" />
      <el-table-column prop="sendNum" label="出货数量" width="120" />
      <el-table-column prop="perPrice" label="单价" width="120" />
      <el-table-column prop="money" label="金额" width="120" />
      <el-table-column prop="sign" label="回签状态" width="120" />
      <!-- <template slot-scope="scope">
          <span v-if="scope.row.sign === true">已回签</span>
          <span v-else>未回签</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="singlePrint(scope.row)">打印</el-button>
          <el-button type="success" size="mini" @click="updated(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="form.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="form.count"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <edItDialog :row="row" :dialog="edItDialog" @init="initTable" />
  </div>
</template>

<script>
import { export2Excel } from '@/utils/common'
import { getShipDaily } from '@/api/order/shipdaily'
import { getUser } from '@/api/order/customerOrder'
import edItDialog from '@/views/order/shipdaily/edit'
export default {
  name: 'ProDaily',
  components: { edItDialog },
  data() {
    return {
      edItDialog: {
        show: false
      },
      tableData: [],
      total: 0,
      form: {
        page: 1,
        count: 10,
        shipDate: null,
        name: '',
        outNo: ''
      },
      select: [],
      name: '',
      row: {},
      tempArray: []
    }
  },
  created() {
    getUser().then(res => {
      this.name = res.nickname
    })
    this.initTable()
    var date = new Date()
    this.now = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
  },
  methods: {
    updated(row) {
      this.edItDialog.show = true
      this.row = row
    },
    query() {
      this.initTable()
    },
    initTable() {
      // if (this.form.shipDate != null) {
      //   this.form.shipDate = this.form.shipDate.toString()
      // }
      getShipDaily(this.form).then(res => {
        if (res === '') {
          this.tableData = []
          return
        }
        this.tableData = res.list
        this.tableData.forEach(x => {
          this.$set(x, 'shipDate', x.createdTime)
          x.sign = x.sign === true ? '已回签' : '未回签'
          x.money = x.perPrice * x.sendNum
        })
        this.total = res.total
      })
    },
    handleSizeChange(size) {
      this.form.count = size
      this.initTable()
    },
    handleCurrentChange(page) {
      this.form.page = page
      this.initTable()
    },
    toExcel() {
      var list = this.tableData
      const th = ['客户名称', '出货日期', '出货单号', '箱型', '出货数量', '单价', '金额', '回签状态']
      const filterVal = ['name', 'shipDate', 'outNo', 'boxType', 'sendNum', 'perPrice', 'money', 'sign']
      const data = list.map(v => filterVal.map(k => v[k]))
      export2Excel(th, data, '出货日报表')
    },
    print(val) {
      if (this.select.length === 0) {
        this.select = this.tableData
      }
      // 判断是否为相同客户
      for (var i = 0; i < this.tempArray.map(x => x.name).length; i++) {
        for (var j = i + 1; j < this.tempArray.map(x => x.name).length; j++) {
          if (this.tempArray.map(x => x.name)[i] !== this.tempArray.map(x => x.name)[j]) {
            return this.$message.error('只有选择相同客户才能打印!!!')
          } else {
            this.select[0].b = val
          }
        }
      }
      this.$router.push({
        path: '/shipOrder',
        query: this.select
      })
    },
    handleSelectionChange(select) {
      this.select = select
      this.tempArray = select
    },
    singlePrint(row) {
      var list = []
      list.push(row)
      this.$router.push({
        path: '/shipOrder',
        query: list
      })
    }
  }
}
</script>

<style scoped>
.font{
  font-weight: bold;
  width:400px;
  height: 100px;
  line-height: 100px;
  font-size: 30px;
  font-family: 'Courier New', Courier, monospace;
  /* text-align: center; */
  margin-left: 40%;
  margin-bottom: 0%;
}
</style>
